/***
 *
 * 一个用于添加私有属性的库addAttribute.js
 *
 * Daniel
 *
 * 2018.07.30
 *
 * **/

define(["ajax"],function (Ajax) {
    
    var scope = null;
    
    var addAttr = function () {
        
        //对象池
    this.modelArray = [];
    this.modelGroup = {};
        scope = this;
        
    this.originalAttr = {};
    
    };
    
    //初始化数据
    addAttr.prototype.init = function(world,jsonUrl){
        if(world==undefined)return;
        var jsonData;
        this.modelArray = world.modelArray;
        this.modelGroup = world.modelGroup;
        scope.ObjectClear(scope.originalAttr);
        if(jsonUrl)Ajax.init({url:jsonUrl},function (object) {
            jsonData = JSON.parse(object.data);
            if(jsonData.length==0)return;
            var i = jsonData.length-1;
            for(;i>-1;i--){
                var objs = jsonData[i];
                var id = objs.name;
                var mesh = scope.modelGroup[id];
                scope.originalAttr[id]=objs.originalAttr;
                changeMesh(mesh,objs);
            }
            
        });
        
    };
    
    //添加属性
    addAttr.prototype.add = function (object,prames) {
        if(object == undefined || prames == undefined )return;
        changeMesh(object,prames)
    };
    
    //获取属性
    addAttr.prototype.get = function (obj) {
        if(object==undefined)return;
        var obj = {
            name: obj.name,
            color: obj.material.color,
            opacity : obj.material.opacity,
            transparent : obj.material.transparent
        };
        return obj;
    };
    
    //移除属性
    addAttr.prototype.remove = function (object) {
        var prames = scope.originalAttr[object.name];
        if(prames==undefined)return;
            changeMesh(object,prames);
            scope.originalAttr[object.name]=null;
    };
    
    //保存属性
    addAttr.prototype.save = function (obj) {
        if(obj == undefined)return;
        var json = {
            name: obj.name,
            color: obj.material.color,
            opacity : obj.material.opacity,
            transparent : obj.material.transparent,
            originalAttr:scope.originalAttr[obj.name]
        };
        
        return JSON.stringify(json);
    };
    
    //解析属性
    addAttr.prototype.prase = function (json) {
        
        if(json==undefined)return;
        var object = JSON.parse(json);
        var name = object.name;
            changeMesh(name,object);
    };
    
    //对象回收
    addAttr.prototype.ObjectClear = function (object) {
        if(object instanceof Object){
            for(var key in object){
                delete object[key];
            }
        }
    
    };
      
    //更改模型属性
    function changeMesh(obj,object){
        if(obj == undefined)return;
        var mesh;
        obj instanceof Object ? mesh = obj : scope.modelArray.forEach(function(e){if(e.name == name){mesh = e};return});
        if(!recordOrig(mesh.material,mesh.name))return;
        mesh.material.color = object.color;
        mesh.material.opacity = object.opacity;
        mesh.material.transparent = object.flag;
        mesh.material.needsUpdate = true;
    }
    
    //记录模型原始属性
    function recordOrig (material,id){
        if(material == undefined || id == undefined)return false;
        if(scope.originalAttr[id]!==undefined)return;
        scope.originalAttr[id] = {
            name: material.name,
            color: material.color,
            opacity : material.opacity,
            transparent : material.transparent
        };
        
        return true
    }
    
    
    return addAttr;
});